package C_Paper;

import java.util.Arrays;

public class C100_17 {
    public static void main(String[] args) {
        zf(new String[]{"cat","bt","hat","tree"},"a?tach??");
        zf(new String[]{"apple","car","window"},"welldoneapplec??");
    }
    public static void zf(String[] words, String chars){
        int sum = 0;
        char[] cs = chars.toCharArray();
        Arrays.sort(cs);
        for (String word : words) {
            int[] x = new int[chars.length()];
            int xx = 0;
            for (int i = 0; i < word.length() ; i++) {
                char c = word.charAt(i);
                for (int j = x.length - 1; j >= 0; j--) {
                    if (x[j] == 0 && (c == cs[j] || cs[j] == '?')){
                        x[j] = 1;
                        xx++;
                        break;
                    }
                }
            }
            sum += xx == word.length() ? 1 :0;
        }
        System.out.println(sum);
    }
}
